package com.openvas.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.openvas.core.opvas.SSHLinux;
import com.openvas.until.DocumentUtil;
import org.jdom2.Document;
import org.jdom2.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.openvas.mapper.TableUserMapper;
import com.openvas.domain.TableUser;
import com.openvas.service.ITableUserService;
import com.openvas.commons.common.core.text.Convert;

import static com.openvas.until.DocumentUtil.judgeStatus;

/**
 * 用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-04
 */
@Service
public class TableUserServiceImpl implements ITableUserService 
{
    @Autowired
    private TableUserMapper tableUserMapper;
    @Autowired
    private SSHLinux sshLinux;

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public TableUser selectTableUserById(Long id)
    {
        return tableUserMapper.selectTableUserById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param tableUser 用户
     * @return 用户
     */
    @Override
    public List<TableUser> selectTableUserList(TableUser tableUser) throws Exception
    {
        StringBuilder xmlcmd = new StringBuilder();
        xmlcmd.append("<get_users ");
        String xmlString = sshLinux.execSshCMD( "sudo -u _gvm gvm-cli --gmp-username admin --gmp-password admin socket --xml='" + xmlcmd + "'");
        List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
        Document doc = DocumentUtil.getDocument(xmlString);
        Element root = doc.getRootElement();
        judgeStatus(root);//判断返回的状态码
        //获取user子节点列表
        List<Element> userList = root.getChildren("user");
        for (int i = 0; i < userList.size(); i++) {
            TableUser user = new TableUser();
            Element e = userList.get(i);
            String id = e.getAttributeValue("id");
            String name = e.getChildText("name");
            String comment = e.getChildText("comment");
            String owner = e.getChildText("owner");
            String creationTime = e.getChildText("creation_time");
            String modificationTime = e.getChildText("modification_time");
            String roleId = e.getChildText("role_id");
            String roleName = e.getChildText("role_name");
            String comment = e.getChildText("comment");
            String name = e.getChildText("name");
            String comment = e.getChildText("comment");
        return tableUserMapper.selectTableUserList(tableUser);
    }

    /**
     * 新增用户
     * 
     * @param tableUser 用户
     * @return 结果
     */
    @Override
    public int insertTableUser(TableUser tableUser)
    {
        return tableUserMapper.insertTableUser(tableUser);
    }

    /**
     * 修改用户
     * 
     * @param tableUser 用户
     * @return 结果
     */
    @Override
    public int updateTableUser(TableUser tableUser)
    {
        return tableUserMapper.updateTableUser(tableUser);
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteTableUserByIds(String ids)
    {
        return tableUserMapper.deleteTableUserByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteTableUserById(Long id)
    {
        return tableUserMapper.deleteTableUserById(id);
    }
}
